package com.mdl.common.util.oss;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.OSSObject;
import com.mdl.common.exp.BizException;
import com.mdl.common.util.string.StringUtil;

import java.io.InputStream;
import java.net.URLEncoder;

/**
 * @description: 阿里oss图片上传
 * @author: liutianyu
 * @create: 2018/10/23 11:14
 */
public class MdlOssClient {

    //节点
    private String endPoint        = "oss-cn-beijing.aliyuncs.com";
    //访问api权限id
    private String accessKeyId     = "LTAITKK8IjV3akC7";
    //访问api权限密码
    private String accessKeySecret = "JcSzxNgHeZqpxFyIRm7dUu2jJaniPO";
    //存储空间
    private String bucketName      = "mondora-test";
    //文件夹
    private String dir             = "";
    //拼装的url
    private String url;

    /**
     * 构造方法
     *
     * @param endPoint           节点
     * @param accessKeyId        ak
     * @param accessKeySecret    sk
     * @param bucketName         存储空间
     * @param dir                存储目录
     */
    public MdlOssClient(String endPoint, String accessKeyId, String accessKeySecret, String bucketName, String dir) {
        this.endPoint = endPoint;
        this.accessKeyId = accessKeyId;
        this.accessKeySecret = accessKeySecret;
        this.bucketName = bucketName;
        this.url = "https://" + bucketName + "." + endPoint + "/";
        if (!StringUtil.isEmpty(dir)) {
            this.dir = dir;
            this.url = "https://" + bucketName + "." + endPoint + "/" + dir + "/";
        }

    }

    /**
     * 无参数构造方法
     */
    public MdlOssClient() {
        this.url = "https://" + bucketName + "." + endPoint + "/";
    }

    /**
     * 简单上传--文件流
     *
     * @param inputStream
     * @param fileName
     */
    public String upload(InputStream inputStream, String fileName) {
        if (null == inputStream) {
            throw new BizException("900001", "文件不能为空！");
        }
        if (StringUtil.isEmpty(fileName)) {
            throw new BizException("900002", "文件名称为空！");
        }
        String tempFileName = "".equals(dir) ? fileName : dir + "/" + fileName;
        // 创建OSSClient实例。
        OSSClient ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret);

        // 查看文件是否存在
        try {
            // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
            ossClient.getObject(bucketName, tempFileName);
            throw new BizException("900003", "该目录下已经存在名称相同的文件!");
        } catch (OSSException ex) {
            if ("NoSuchKey".equals(ex.getErrorCode())) {
                //说明没找到 不存在 可以存入
            } else {
                ossClient.shutdown();
                throw new BizException(ex.getErrorCode(), ex.getErrorMessage());
            }
        }

        //上传
        try {
            ossClient.putObject(bucketName, tempFileName, inputStream);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message: " + oe.getErrorMessage());
            System.out.println("Error Code:       " + oe.getErrorCode());
            System.out.println("Request ID:      " + oe.getRequestId());
            System.out.println("Host ID:           " + oe.getHostId());

            throw new BizException(oe.getErrorCode() + "", oe.getMessage());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message: " + ce.getMessage());
            throw new BizException(ce.getErrorCode() + "", ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        return url + fileName;
    }

}